var url=require('url');
var request = require('request');
var config=require('../config/config');
var superagent = require('superagent');


module.exports = {
    login: function (req, res, next) {
        // 获取前台页面传过来的参数
        var body = null;
        if(req.query.token!=undefined){
            body=req.query;
        }else{
            body=req.body;
        }
        if(body.token!=undefined){
            superagent
                .get(config.webUrl+'user/token/detail')
                .set('token', body.token)
                .end(function(err, response){
                    if (err) {
                        res.status(200).json({ code: 500, message:'用户信息获取失败'})
                    } else {
                        if(response.body.code==10000){
                            var user={
                                token:body.token,
                                user:response.body.content
                            }
                            req.session.user=user;
                            console.log('登录成功跳转index req.session.user========='+req.session.user);
                            //res.redirect('/');
                            res.status(200).json({ code: 10000, message:'登录成功'})
                        }else{
                            res.status(200).json({ code: 500, message:'用户信息获取失败'})
                        }
                    }
                });
        }else{
            res.jsonp({ success: '200',  'message': '没有token!', 'body':body});
        }
    },
    ranking: function (req, res, next) {
        superagent
            .get(config.webUrl+'ranking/list')
            .set('token', req.session.user.token)
            .end(function(err, response){
                if (err) {
                    res.status(200).json({ code: 500})
                } else {
                    if(response.body.code==10000){
                        res.status(200).json(response.body);
                    }else{
                        res.status(200).json({ code: 500})
                    }
                }
            });
    },
    solve: function (req, res, next) {
        superagent
            .get(config.webUrl+'solve/recent')
            .set('token', req.session.user.token)
            .end(function(err, response){
                if (err) {
                    res.status(200).json({ code: 500})
                } else {
                    if(response.body.code==10000){
                        res.status(200).json(response.body);
                    }else{
                        res.status(200).json({ code: 500})
                    }
                }
            });
    },
    rankingId: function (req, res, next) {
        var body = null;
        if(req.query.id!=undefined){
            body=req.query;
        }else{
            body=req.body;
        }
        superagent
            .get(config.webUrl+'ranking/findById')
            .query({ userId: body.id})
            .set('token', req.session.user.token)
            .end(function(err, response){
                if (err) {
                    res.status(200).json({ code: 500})
                } else {
                    if(response.body.code==10000){
                        res.status(200).json(response.body);
                    }else{
                        res.status(200).json({ code: 500})
                    }
                }
            });
    },
    solveSelect: function (req, res, next) {
        var body = null;
        if(req.query.id!=undefined){
            body=req.query;
        }else{
            body=req.body;
        }
        superagent
            .get(config.webUrl+'solve/select')
            .query({ id: body.id})
            .set('token', req.session.user.token)
            .end(function(err, response){
                if (err) {
                    res.status(200).json({ code: 500})
                } else {
                    if(response.body.code==10000){
                        res.status(200).json(response.body);
                    }else{
                        res.status(200).json({ code: 500})
                    }
                }
            });
    },
    likes: function (req, res, next) {
        var body = null;
        if(req.query.id!=undefined){
            body=req.query;
        }else{
            body=req.body;
        }
        superagent
            .get(config.webUrl+'solve/likes')
            .query({ solveId: body.id})
            .set('token', req.session.user.token)
            .end(function(err, response){
                if (err) {
                    res.status(200).json({ code: 500})
                } else {
                    if(response.body.code==10000){
                        res.status(200).json(response.body);
                    }else{
                        res.status(200).json({ code: 500})
                    }
                }
            });
    },
    collectionAdd: function (req, res, next) {
        var body = null;
        if(req.query.type!=undefined){
            body=req.query;
        }else{
            body=req.body;
        }
        superagent
            .post(config.webUrl+'user/collection')
            .query({id:body.id, type: body.type})
            .set('token', req.session.user.token)
            .end(function(err, response){
                if (err) {
                    res.status(200).json({ code: 500})
                } else {
                    if(response.body.code==10000){
                        res.status(200).json(response.body);
                    }else{
                        res.status(200).json({ code: 500})
                    }
                }
            });
    },
    collectionDel: function (req, res, next) {
        var body = null;
        if(req.query.type!=undefined){
            body=req.query;
        }else{
            body=req.body;
        }
        superagent
            .post(config.webUrl+'user/collection/delete')
            .query({id:body.id,  type: body.type})
            .set('token', req.session.user.token)
            .end(function(err, response){
                if (err) {
                    res.status(200).json({ code: 500})
                } else {
                    if(response.body.code==10000){
                        res.status(200).json(response.body);
                    }else{
                        res.status(200).json({ code: 500})
                    }
                }
            });
    },
};